<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Message\Controllers;

use App\Eloquent\Ygt\BuyersInvoice;
use App\Eloquent\Ygt\Customer;
use App\Eloquent\Ygt\CustomerOrderPrice;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Order;
use App\Eloquent\Ygt\OrderType;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\ProductFields;
use App\Eloquent\Ygt\Purchase;
use App\Eloquent\Ygt\PurchaseMaterial;
use App\Eloquent\Ygt\SellerCompany;
use App\Eloquent\Ygt\UserMessage;
use App\Eloquent\Ygt\Warehouse;
use App\Eloquent\Ygt\WarehouseLog;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class WarehouseController extends Controller
{
    public $userId          = 25;

    //33=>'成品入库',//成品入库时入库人给所有销售发消息,目的:通过消息详情可以去生成应收单
    public  function in()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $messageId          = request('message_id',0);
        if(!$userInfo || !$messageId)
        {
            xThrow( ERR_PARAMETER );
        }
        //消息内容
        $where              = ['id'=>$messageId];
        $message            = UserMessage::getInfo($where);
        if(!$message)
        {
            xThrow(ERR_UNKNOWN,'消息出错');
        }
        //消息关联的成品
        $type               = $message->type;
        if($type!=33)
        {
            xThrow(ERR_UNKNOWN,'消息出错');
        }
        $typeId             = $message->type_id;
        $where              = ['id'=>$typeId];
        $warehouseLogInfo   = WarehouseLog::getInfo($where);
        if(!$warehouseLogInfo)
        {
            xThrow(ERR_UNKNOWN,'成品信息出错');
        }
        $nowNumber          = $warehouseLogInfo->now_number;
        //成品关联的工单
        $orderId            = $warehouseLogInfo->order_id;
        $orderInfo          = Order::where('id',$orderId)->first();
        if(!$orderInfo)
        {
            xThrow(ERR_UNKNOWN,'工单信息出错');
        }
        $orderArr           = $orderInfo->toArray();
        $orderContent       = \App\Engine\OrderEngine::dealOrderFiled($orderArr);
        $lists[]            = [
            'title'=>'入库信息',
            'data'=>[
                ['left'=>'工单类型','right'=>$orderContent['order_type_title']],
                ['left'=>'单位名称','right'=>$orderContent['field_name_23']],
                ['left'=>'品名','right'=>$orderContent['product_name']],
                ['left'=>'工单号','right'=>$orderContent['order_title']],
                ['left'=>'成品规格','right'=>$orderContent['finished_specification']],
                ['left'=>'入库数量','right'=>$nowNumber],
            ],
        ];
        //工单关联的客户
        $customerId         = $orderInfo->customer_name;
        $where              = ['id'=>$customerId];
        $customerInfo       = Customer::getInfo($where);
        if(!$customerInfo)
        {
            xThrow(ERR_UNKNOWN,'客户信息出错');
        }
        $imgPath            = Func::getImgUrlById($customerInfo->avatars_id);
        $customer           = [
            'customer_name'=>$customerInfo->customer_name,
            'img_path'=>$imgPath,
        ];
        $result             = [
            'type'=>$type,
            'customer'=>$customer,
            'lists'=>$lists,
        ];
        return $result;
    }
}